package org.orbis.index;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.orbis.communicate.MainService;
import org.orbis.structure.ContentInfo;

public class IndexServer extends HttpServlet{
	private static final long serialVersionUID = 7259834172675048285L;

	private void doSomething(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		MainService postService = MainService.getInstance();
		ContentInfo contentInfo = postService.contentInfo();
		IndexingPackage pkg = new IndexingPackage(contentInfo);

		if(req.getParameterMap().size() == 0){
			resp.setCharacterEncoding("UTF-8");
			// If no parameter posted, Make dialogue indexing
			new IndexForm(contentInfo, resp.getWriter()).putForm();
			return;
		}

		// Content: required
		for(int i = 0; i < contentInfo.contentsNum(); i++){
			String param = req.getParameter(contentInfo.contentName(i));
			if(param == null){
				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Entity of content \""
					+ contentInfo.contentName(i) + "\" needed");
				return;
			}
			// ISO-8859-1 -> UTF-8
//			param = new String(param.getBytes(Charset.forName("iso-8859-1")), Charset.forName("UTF-8"));
			pkg.setContent(param, i);
		}

		// Appendix: required
		for(int i = 0; i < contentInfo.appendixNum(); i++){
			String param = req.getParameter(contentInfo.appendixName(i));
			if(param == null){
				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Entity of appendix \""
					+ contentInfo.appendixName(i) + "\" needed");
				return;
			}
			// ISO-8859-1 -> UTF-8
//			param = new String(param.getBytes(Charset.forName("iso-8859-1")), Charset.forName("UTF-8"));
			pkg.setAppendix(param, i);
		}

		// Binary field: option
		for(int i = 0; i < contentInfo.binaryFlagNum(); i++){
			String param = req.getParameter(contentInfo.binaryFlagName(i));
			try{
				boolean boolval = false;
				if(param != null) boolval = Boolean.parseBoolean(param);
				pkg.setBinaryFlag(boolval, i);
			}catch(Exception e){
				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
					"Boolean must be \"true\" or \"false\"");
				return;
			}
		}

		// Integer flag field: option
		for(int i = 0; i < contentInfo.integerFlagNum(); i++){
			String param = req.getParameter(contentInfo.integerFlagName(i));
			try{
				int intval = 0;
				if(param != null) intval = Integer.parseInt(param);
				pkg.setIntFlag(intval, i);
			}catch(Exception e){
				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
					"Value must be numeric, encountered non-digit value");
				return;
			}
		}

		// Long flag field: option
		for(int i = 0; i < contentInfo.longFlagNum(); i++){
			String param = req.getParameter(contentInfo.longFlagName(i));
			try{
				long longval = 0L;
				if(param != null) longval = Long.parseLong(param);
				pkg.setLongFlag(longval, i);
			}catch(Exception e){
				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
					"Value must be numeric, encountered non-digit value");
				return;
			}
		}

		// Float flag field: option
		for(int i = 0; i < contentInfo.floatFlagNum(); i++){
			String param = req.getParameter(contentInfo.floatFlagName(i));
			try{
				float floatval = 0.0F;
				if(param != null) floatval = Float.parseFloat(param);
				pkg.setFloatFlag(floatval, i);
			}catch(Exception e){
				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
					"Value must be numeric, encountered non-digit value");
				return;
			}
		}

		postService.postIndex(pkg);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
		IOException{
		doSomething(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
		IOException{
		doSomething(req, resp);
	}
}
